<?php

namespace app\models\search;

use Yii;
use yii\data\ActiveDataProvider;

class AssignmentSearch extends \mdm\admin\models\searchs\Assignment
{
    /**
     * Create data provider for Assignment model.
     * @param  array                        $params
     * @param  \yii\db\ActiveRecord         $class
     * @param  string                       $usernameField
     * @return \yii\data\ActiveDataProvider
     */
    public function search($params, $class, $usernameField)
    {
        $query = $class::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        //新增这个参数 concat_ws('#',`item_name`,`id`)  as pm 来让其作为唯一的主键，不要让yii把其重复的数据过滤掉重复的
        $query->select("concat_ws('#',`item_name`,`id`) as pm,user.*,auth_assignment.*,auth_item.type")->indexBy("pm");//必须加这个，否则join得到的是，缺少查询出来的字段 SELECT * FROM `user` LEFT JOIN `auth_assignment` ON auth_assignment.user_id=user.id
        $query->leftJoin('auth_assignment', 'auth_assignment.user_id=user.id',"")->asArray();//新增的新增一个aggignment的字段 ,asArray(),必须要，否则显示不出来多余字段
        $query->leftJoin('auth_item', 'auth_assignment.item_name=auth_item.name',"");//新增的新增一个item的字段 ,asArray(),必须要，否则显示不出来多余字段
        $query->andWhere('type != 2 or type is null');//不等于2,然后加上空的
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere(['like', $usernameField, $this->username]);

        return $dataProvider;
    }

}
